import 'package:flutter/material.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/widget/load_image.dart';
import 'package:flutter_shop/widget/tree_select/tree_node.dart';
import 'package:flutter_shop/widget/water_click.dart';

/// Title:TreeItemWidget <br/>
/// Description: <br/>
/// author:xiemiao <br/>
/// date: 2020-04-16  <br/>
/// version 1.0 <br/>
class TreeItemWidget extends StatelessWidget {
  ///缩进值
  static const double RETRACT = 15.0;

  final TreeNode item;
  final Function(TreeNode) checkboxClick;

  TreeItemWidget(this.item, {this.checkboxClick});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: Paddings.common,
      child: Row(
        children: [
          _leftArrow(),
          _checkbox(),
          Expanded(child: _label()),
        ],
      ),
    );
  }

  ///左箭头
  Widget _leftArrow() {
    //是否展示左箭头
    bool showLeftArrow = !item.isLeaf();

    //箭头图标
    String icon = item.isExpand ? R.assetsImgCommonIconDownArrow : R.assetsImgCommonIconRightArrow;

    //与左边间距
    double left = item.level * RETRACT;

    return Container(
      width: 17,
      height: 17,
      margin: EdgeInsets.only(left: left),
      child: showLeftArrow ? LoadImage(icon) : Gaps.empty,
    );
  }

  ///checkbox
  Widget _checkbox() {
    //选中图片

    String icon = R.assetsImgCommonIconNodeUncheck;
    if (item.checkStatus == TreeNode.CHECK_STATUS_ARR[0]) {
      //未选
      icon = R.assetsImgCommonIconNodeUncheck;
    } else if (item.checkStatus == TreeNode.CHECK_STATUS_ARR[1]) {
      //已选
      icon = R.assetsImgCommonIconNodeChecked;
    } else if (item.checkStatus == TreeNode.CHECK_STATUS_ARR[2]) {
      //半选
      icon = R.assetsImgCommonIconNodeHalfcheck;
    }

    return item.isCanCheck()
        ? WaterClick(
            width: 20,
            height: 20,
            onPressed: () {
              if (this.checkboxClick != null) {
                checkboxClick(item);
              }
            },
            child: LoadImage(
              icon,
              width: 15,
              height: 15,
            ),
          )
        : Gaps.empty;
  }

  ///标题
  Widget _label() {
    return Container(
      child: Text(
        item.get_label(),
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: CommonDimens.font_size_middle,
          color: CommonColors.common_text,
        ),
      ),
    );
  }
}
